/**
 * 
 */
package com.baidu.ueditor.upload;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import com.baidu.ueditor.define.AppInfo;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.FileType;
import com.baidu.ueditor.define.State;
import com.hzlh.controller.LoginController;
import com.hzlh.service.FtpAndOssService;

/**
 * Title:LinkerUpload 
 * description: 
 * company: linker
 * @author zhanjp
 * @date 2016年11月9日 下午7:26:25
 */
public class LinkerUploader {

	public static final State save(HttpServletRequest request,
			Map<String, Object> conf) {
		try {
			MultipartRequest req2 = (MultipartRequest) request;
			MultipartFile file = req2.getFile("upfile");
			String originFileName = file.getOriginalFilename();
			String suffix = FileType.getSuffixByFilename(originFileName);
			
			State storageState = new BaseState(true);
			storageState.putInfo("size", file.getSize());
			storageState.putInfo("title", originFileName);
			String appType = "yundao";
			String url1 = new FtpAndOssService().uploadLocalFileToOss("file", appType, file);
			String url = url1;
//			+"?x-oss-process=image/resize,w_320/auto-orient,0"//加上这个参数将图片设置为最宽320
			if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
				return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
			}
			
			storageState.putInfo("url", url);
			storageState.putInfo("type", suffix);
			storageState.putInfo("original", originFileName + suffix);
			
			return storageState;
		} catch (Exception e) {
			e.printStackTrace();
			return new BaseState(false, AppInfo.IO_ERROR);
		}
	}
	
	private static boolean validType(String type, String[] allowTypes) {
		List<String> list = Arrays.asList(allowTypes);

		return list.contains(type);
	}
}
